package com.itheima.pinda.web.truck;

import com.itheima.pinda.DTO.truck.TruckTypeDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.truck.PdTruckType;
import com.itheima.pinda.service.PdTruckTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.List;

/**
 * 车辆类型管理
 *
 * @author llhuang10
 * @date 2021/10/2 21:18
 */
@Slf4j
@Validated
@Api(tags = "车辆类型管理")
@RestController
@RequestMapping("/base/truck/type")
public class TruckTypeController {

    @Resource
    private PdTruckTypeService pdTruckTypeService;


    /**
     * 添加车辆类型
     *
     * @param dto 车辆类型信息
     * @return 车辆类型信息
     */
    @PostMapping
    @ApiOperation("添加车辆类型")
    public TruckTypeDto saveTruckType(@Valid @RequestBody TruckTypeDto dto) {
        return pdTruckTypeService.saveTruckType(dto);
    }

    /**
     * 根据id获取车辆类型详情
     *
     * @param id 车辆类型id
     * @return 车辆类型信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取车辆类型详情")
    public TruckTypeDto fineById(@NotBlank(message = "车辆类型Id不能为空") @PathVariable String id) {
        return pdTruckTypeService.findById(id);
    }


    /**
     * 获取车辆类型分页数据
     *
     * @param page            页码
     * @param pageSize        页尺寸
     * @param name            车辆类型名称
     * @param allowableLoad   车辆载重
     * @param allowableVolume 车辆体积
     * @return 车辆类型分页数据
     */
    @GetMapping("/page")
    @ApiOperation("获取车辆类型分页数据")
    public PageResponse<TruckTypeDto> findByPage(@RequestParam(name = "page") Integer page,
                                                 @RequestParam(name = "pageSize") Integer pageSize,
                                                 @RequestParam(name = "name", required = false) String name,
                                                 @RequestParam(name = "allowableLoad", required = false) BigDecimal allowableLoad,
                                                 @RequestParam(name = "allowableVolume", required = false) BigDecimal allowableVolume) {
        return pdTruckTypeService.findByPage(page, pageSize, name, allowableLoad, allowableVolume);
    }

    /**
     * 获取车辆类型列表
     *
     * @param ids 车辆类型id
     * @return 车辆类型列表
     */
    @GetMapping
    @ApiOperation("获取车辆类型列表")
    public List<TruckTypeDto> findAll(@RequestParam(name = "ids", required = false) List<String> ids) {
        return pdTruckTypeService.findAll(ids);
    }

    /**
     * 根据Id更新车辆类型信息
     *
     * @param id  车辆类型id
     * @param dto 车辆类型信息
     * @return 车辆类型信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新车辆类型信息")
    public TruckTypeDto update(@NotBlank(message = "车辆类型Id不能为空") @PathVariable String id,
                               @RequestBody TruckTypeDto dto) {
        return pdTruckTypeService.updateTruckType(id, dto);
    }

    /**
     * 根据Id删除车辆类型
     *
     * @param id 车辆类型Id
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    @ApiOperation("根据Id删除车辆类型")
    public Result disable(@NotBlank(message = "车辆类型Id不能为空") @PathVariable(name = "id") String id) {
        // todo 待实现, 是否关联数据
        PdTruckType pdTruckType = new PdTruckType();
        pdTruckType.setId(Long.parseLong(id));
        pdTruckType.setStatus(Constant.DATA_DISABLE_STATUS);
        boolean result = pdTruckTypeService.updateById(pdTruckType);
        return result ? Result.ok() : Result.error("删除失败!");
    }
}
